package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName SliderDemo
 * @Description 进度条
 * @Author wangss
 * @date 2020.05.26 10:31
 * @Version 1.0
 */
public class SliderDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Slider slider = new Slider(0, 100, 0);

        slider.setPrefWidth(400d);
        slider.setShowTickLabels(true);
        slider.setShowTickMarks(true);
        slider.setMajorTickUnit(10);

//        slider.setOrientation(Orientation.VERTICAL);

        // 可以更改展示
        slider.setLabelFormatter(new StringConverter<Double>() {
            @Override
            public String toString(Double object) {
                if (object == 0) {
                    return "零";
                } else if (object == 10) {
                    return "十";
                }
                return object.toString();
            }

            @Override
            public Double fromString(String string) {
                return null;
            }
        });

        AnchorPane.setTopAnchor(slider, 10d);
        AnchorPane.setLeftAnchor(slider, 20d);
        anchorPane.getChildren().add(slider);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {

            }
        });

        slider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                System.out.println(newValue);
            }
        });

        slider.valueChangingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

            }
        });

        // 这样直接开线程不好，这里只是演示
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 100; i++) {
                    try {
                        slider.setValue(i);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
